package org.apogee.mil.server.login;

import org.apache.log4j.Logger;
import org.apogee.mil.client.services.login.LoginService;
import org.apogee.mil.server.database.DatabaseDriver;
import org.apogee.mil.shared.User;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class LoginServiceImpl extends RemoteServiceServlet implements LoginService{


	private static Logger logger = Logger.getLogger(LoginServiceImpl.class);
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private DatabaseDriver db;

	@Override
	public boolean initialize() {
		db = new DatabaseDriver();
		return db.initialize();
	}

	@Override
	public User login(String username, String passwd) {
		
		return db.login(username, passwd);
	}

	@Override
	public boolean changePassword(int userid, String currentPwd, String newPwd) {
		String sql = "UPDATE users SET password='" +newPwd + "' WHERE user_id=" + userid 
				+ " and password='"+currentPwd+"'";
		return db.executeUpdate(sql);
	}

	@Override
	public boolean close() {
		try {
			return db.close();
		} catch (NullPointerException ex) {
			logger.error(ex.getMessage());
			return false;
		}
	}

}
